/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.inf.ufg.view.cadastros;

import br.inf.ufg.model.PontoVendas;
import br.inf.ufg.model.PontoVendasDAO;
import br.inf.ufg.model.TipoAssentos;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author umarley.faria
 */
public class TelaCadastroPontoVendas extends javax.swing.JFrame {

    /**
     * Creates new form TelaCadastroLocal
     */
    public TelaCadastroPontoVendas() {
        initComponents();
        iniciarBD();
        primeiro();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        btFirst = new javax.swing.JButton();
        btPrev = new javax.swing.JButton();
        btNext = new javax.swing.JButton();
        btLast = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        btNovo = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        btEditar = new javax.swing.JButton();
        btDeletar = new javax.swing.JButton();
        btPesquisar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        tfRua = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        tfBairro = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        tfCidade = new javax.swing.JTextField();
        tfComplemento = new javax.swing.JTextField();
        cbUF = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        tfCpfCnpj = new javax.swing.JTextField();
        tfRazaoSocial = new javax.swing.JTextField();
        tfNomeFantasia = new javax.swing.JTextField();
        tfCodigo = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        tfCep = new javax.swing.JTextField();
        ckAtivo = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro de Ponto de Vendas");
        setAlwaysOnTop(true);

        jToolBar1.setRollover(true);

        btFirst.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/resultset_first.png"))); // NOI18N
        btFirst.setToolTipText("Primeiro Registro");
        btFirst.setFocusable(false);
        btFirst.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btFirst.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btFirst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btFirstActionPerformed(evt);
            }
        });
        jToolBar1.add(btFirst);

        btPrev.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/resultset_previous.png"))); // NOI18N
        btPrev.setToolTipText("Anterior");
        btPrev.setFocusable(false);
        btPrev.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btPrev.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btPrev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPrevActionPerformed(evt);
            }
        });
        jToolBar1.add(btPrev);

        btNext.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/resultset_next.png"))); // NOI18N
        btNext.setToolTipText("Próximo");
        btNext.setFocusable(false);
        btNext.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btNext.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNextActionPerformed(evt);
            }
        });
        jToolBar1.add(btNext);

        btLast.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/resultset_last.png"))); // NOI18N
        btLast.setToolTipText("Último Registro");
        btLast.setFocusable(false);
        btLast.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btLast.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btLast.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLastActionPerformed(evt);
            }
        });
        jToolBar1.add(btLast);
        jToolBar1.add(jSeparator1);

        btNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/add.png"))); // NOI18N
        btNovo.setToolTipText("Novo");
        btNovo.setFocusable(false);
        btNovo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btNovo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });
        jToolBar1.add(btNovo);

        btSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/disk.png"))); // NOI18N
        btSalvar.setToolTipText("Salvar");
        btSalvar.setFocusable(false);
        btSalvar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btSalvar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });
        jToolBar1.add(btSalvar);

        btEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/pencil.png"))); // NOI18N
        btEditar.setToolTipText("Editar");
        btEditar.setFocusable(false);
        btEditar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btEditar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditarActionPerformed(evt);
            }
        });
        jToolBar1.add(btEditar);

        btDeletar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/delete.png"))); // NOI18N
        btDeletar.setToolTipText("Remover");
        btDeletar.setFocusable(false);
        btDeletar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btDeletar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btDeletar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDeletarActionPerformed(evt);
            }
        });
        jToolBar1.add(btDeletar);

        btPesquisar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/magnifier.png"))); // NOI18N
        btPesquisar.setToolTipText("Procurar");
        btPesquisar.setFocusable(false);
        btPesquisar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btPesquisar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisarActionPerformed(evt);
            }
        });
        jToolBar1.add(btPesquisar);

        jLabel1.setText("Código:");

        jLabel2.setText("Razão Social:");

        jLabel3.setText("Nome Fantasia:");

        jLabel4.setText("CPF / CNPJ:");

        jLabel5.setText("Rua:");

        jLabel7.setText("Bairro:");

        jLabel6.setText("Complemento:");

        jLabel8.setText("Cidade:");

        cbUF.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "selecione", "AC", "AL", "AM", "AP", "BA", "CE", "DF", "ES", "GO", "MA", "MG", "MS", "MT", "PA", "PB", "PE", "PI", "PR", "RJ", "RN", "RO", "RR", "RS", "SC", "SE","SP", "TO"}));

        jLabel9.setText("UF:");

        tfRazaoSocial.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfRazaoSocialActionPerformed(evt);
            }
        });

        tfCodigo.setEditable(false);

        jLabel10.setText("CEP");

        tfCep.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfCepActionPerformed(evt);
            }
        });

        ckAtivo.setText("Ativo");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel9))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(cbUF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(17, 17, 17)
                                        .addComponent(tfBairro, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(26, 26, 26)
                                .addComponent(tfRua, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel8)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tfComplemento)
                            .addComponent(tfCidade, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                            .addComponent(tfCep)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(tfCpfCnpj, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(tfRazaoSocial, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(tfNomeFantasia, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(tfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(ckAtivo)))))
                        .addGap(0, 33, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ckAtivo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(tfRazaoSocial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfNomeFantasia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tfCpfCnpj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(tfComplemento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tfRua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel5)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel8)
                        .addComponent(tfCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel7)
                        .addComponent(tfBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbUF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel10)
                        .addComponent(tfCep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 45, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
        // # cadastro de um novo ponto de venda
        novo(); 
    }//GEN-LAST:event_btNovoActionPerformed

    private void tfRazaoSocialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfRazaoSocialActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfRazaoSocialActionPerformed

    private void btFirstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btFirstActionPerformed
        // # primeiro registro
        primeiro();
    }//GEN-LAST:event_btFirstActionPerformed

    private void btPrevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPrevActionPerformed
        // # registro anterior
        anterior(); 
    }//GEN-LAST:event_btPrevActionPerformed

    private void btNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNextActionPerformed
        // # proximo registro
        proximo(); 
    }//GEN-LAST:event_btNextActionPerformed

    private void btLastActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLastActionPerformed
        // # ultimo registro 
        ultimo(); 
    }//GEN-LAST:event_btLastActionPerformed

    private void tfCepActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfCepActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfCepActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        salvar(); 
    }//GEN-LAST:event_btSalvarActionPerformed

    private void btEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditarActionPerformed
        habilitaCampos();
    }//GEN-LAST:event_btEditarActionPerformed

    private void btPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisarActionPerformed
         pesquisar();
    }//GEN-LAST:event_btPesquisarActionPerformed

    private void btDeletarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDeletarActionPerformed
        
        try 
        {
            remover();
        }
        catch (SQLException ex) 
        {
            Logger.getLogger(TelaCadastroPontoVendas.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }//GEN-LAST:event_btDeletarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaCadastroPontoVendas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaCadastroPontoVendas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaCadastroPontoVendas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaCadastroPontoVendas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TelaCadastroPontoVendas().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btDeletar;
    private javax.swing.JButton btEditar;
    private javax.swing.JButton btFirst;
    private javax.swing.JButton btLast;
    private javax.swing.JButton btNext;
    private javax.swing.JButton btNovo;
    private javax.swing.JButton btPesquisar;
    private javax.swing.JButton btPrev;
    private javax.swing.JButton btSalvar;
    private javax.swing.JComboBox cbUF;
    private javax.swing.JCheckBox ckAtivo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTextField tfBairro;
    private javax.swing.JTextField tfCep;
    private javax.swing.JTextField tfCidade;
    private javax.swing.JTextField tfCodigo;
    private javax.swing.JTextField tfComplemento;
    private javax.swing.JTextField tfCpfCnpj;
    private javax.swing.JTextField tfNomeFantasia;
    private javax.swing.JTextField tfRazaoSocial;
    private javax.swing.JTextField tfRua;
    // End of variables declaration//GEN-END:variables

private PontoVendasDAO pontoDAO;

private void iniciarBD() {
        
        
        try {
            pontoDAO = new PontoVendasDAO();
        } catch ( Exception e ) {
            JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
        }
 }  

    private void remover() throws SQLException
    {
        int resposta = JOptionPane.showConfirmDialog(rootPane, "Deseja Realmente Excluir este registro?", "Pergunta", JOptionPane.CANCEL_OPTION);

        if(resposta == JOptionPane.OK_OPTION)
        {
            
            PontoVendas ponto = new PontoVendas();
            ponto.setIdPonto( Integer.parseInt( tfCodigo.getText() ) );

            try
            {
                pontoDAO.excluirRegistro( ponto );
                primeiro();
            }
            catch(SQLException e)
            {
                JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
            }
        }
    }


    private void pesquisar()
    {
    
        String criterio = JOptionPane.showInputDialog( this, "Digite o código ou o NOME FANTASIA para pesquisar");

        if( !criterio.isEmpty() )
        {
            if( criterio.matches("^\\d+$") )
            {
                
                int codigo = Integer.parseInt( criterio );

                try
                {
                    // # DAO 
                    PontoVendasDAO pontoVendasD = new PontoVendasDAO(); 
                    
                    PontoVendas ponto = pontoVendasD.getPontoVenda(codigo);
                
                    if ( ponto != null ) 
                    {
                        mostrarDados( ponto );                    
                        JOptionPane.showMessageDialog( this, "Registro Encontrado." );
                    }
                    else 
                    {
                        JOptionPane.showMessageDialog( this, "Registro Não Encontrado ");
                    }
                }
                catch(Exception e)
                {
                      JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
                } 


            }
            else
            {
                 try
                 {
                    // # DAO 
                    //PontoVendasDAO pontoDAO = new PontoVendasDAO(); 
                    
                    PontoVendas ponto = pontoDAO.pesquisarPontoVenda(criterio);  
                    
                    if ( ponto != null ) 
                    {
                        mostrarDados( ponto );                    
                        JOptionPane.showMessageDialog( this, "Registro Encontrado." );
                    }
                    else 
                    {
                        JOptionPane.showMessageDialog( this, "Registro Não Encontrado ");
                    }
                    
                }
                catch(Exception e)
                {
                     JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
                }
            }
        }
    }

    public void mostrarDados( PontoVendas ponto ) 
    {
        try
        {
            int idPonto           = ponto.getIdPonto();
            String razaoSocial    = ponto.getRazaoSocial(); 
            String nomeFantasia   = ponto.getNomeFantasia();
            String cpfCnpj        = ponto.getCfpCnpj(); 
            String cep            = ponto.getCep(); 
            String rua            = ponto.getRua(); 
            String complemento    = ponto.getComplemento();
            String cidade         = ponto.getCidade();
            String estado         = ponto.getEstado(); 
            String bairro         = ponto.getBairro(); 
            int status            = ponto.getIsAtivo(); 

            tfCodigo.setText( String.valueOf( idPonto ) );
            tfRazaoSocial.setText( razaoSocial );
            tfNomeFantasia.setText(nomeFantasia);
            tfBairro.setText( bairro );
            tfCpfCnpj.setText( cpfCnpj );
            tfCep.setText( cep );
            tfRua.setText( rua );
            tfComplemento.setText( complemento );
            tfCidade.setText( cidade );
            cbUF.setSelectedItem( estado );
            tfBairro.setText( bairro );    
        }
        catch (Exception e) 
        {
            JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
        }

     }

    private void desabilitaCampos()
    {
         // # setar todos os itens como editaveis 
        tfCodigo.setEditable(false);
        tfRazaoSocial.setEditable(false); 
        tfNomeFantasia.setEditable(false);
        tfCpfCnpj.setEditable(false); 
        tfBairro.setEditable(false);
        tfCidade.setEditable(false);
        tfComplemento.setEditable(false);
        tfRua.setEditable(false); 
        tfCep.setEditable(false);
    }
    
    private void habilitaCampos()
    {
         // # setar todos os itens como editaveis 
        tfCodigo.setEditable(false);
        tfRazaoSocial.setFocusable(rootPaneCheckingEnabled);
        tfRazaoSocial.setEditable(true); 
        tfNomeFantasia.setEditable(true);
        tfCpfCnpj.setEditable(true); 
        tfBairro.setEditable(true);
        tfCidade.setEditable(true);
        tfComplemento.setEditable(true);
        tfRua.setEditable(true); 
        tfCep.setEditable(true);
    }
    
    public void primeiro() 
    {
        desabilitaCampos();
        
        try 
        {
            PontoVendas ponto = pontoDAO.primeiro();

            if ( ponto != null) 
            {
                mostrarDados( ponto );
            }                    

        }
        catch ( SQLException e ) 
        {
            JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
        }
    }

    public void anterior() 
    {
        
        desabilitaCampos(); 
                
        try 
        {
            PontoVendas ponto = pontoDAO.anterior();

            if ( ponto != null) 
            {
                mostrarDados( ponto );
            }                    

        } 
        catch ( SQLException e ) 
        {
            JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
        }

    }

    public void proximo() 
    {
        desabilitaCampos(); 
        
        try 
        {
           PontoVendas ponto = pontoDAO.proximo();

           if ( ponto != null) 
           {
               mostrarDados( ponto );
           }                    

       } 
       catch ( SQLException e ) 
       {
           JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
       }

    }

    public void ultimo() 
    {
        desabilitaCampos();
        
        try 
        {
            PontoVendas ponto = pontoDAO.ultimo();

            if ( ponto != null) 
            {
                mostrarDados( ponto );
            }                    

        } 
        catch ( SQLException e ) 
        {
          JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
        }
    }

    public void novo()
    {
        // # setar todos os itens como editaveis 
        habilitaCampos(); 
        
        // # setar os valores como vazio 
        tfCodigo.setText(null);
        tfNomeFantasia.setText("");
        tfRazaoSocial.setText(""); 
        tfCpfCnpj.setText(""); 
        tfBairro.setText("");
        tfCidade.setText("");
        tfComplemento.setText("");
        tfRua.setText(""); 
        tfCep.setText("");
    }

    // # funcao que salva o registro 
    public void salvar()
    {

        PontoVendas oPontoVendas  = new PontoVendas(); 

       // # setar os valores para o objeto      
        oPontoVendas.setNomeFantasia( tfNomeFantasia.getText() );
        oPontoVendas.setCfpCnpj( tfCpfCnpj.getText() );
        oPontoVendas.setCep( tfCep.getText() );

        oPontoVendas.setCidade( tfCidade.getText() );
        oPontoVendas.setComplemento(tfComplemento.getText() );
        oPontoVendas.setEstado( cbUF.getSelectedItem().toString() );

        oPontoVendas.setIsAtivo( 1 ); // salva o registro sempre como ativo
        oPontoVendas.setRazaoSocial( tfRazaoSocial.getText() ); 
        oPontoVendas.setRua( tfRua.getText() ); 
        oPontoVendas.setBairro( tfBairro.getText() ); 

        // # verificar se e uma inclusao ou edicao de um registro
        if( tfCodigo.getText().isEmpty() )
        {            
            try
            {   
                // # instancia do DAO 
                //PontoVendasDAO pontosVendasD = new PontoVendasDAO();    
                
                pontoDAO.salvarRegistro( oPontoVendas );
                JOptionPane.showMessageDialog( this, "Salvo com Sucesso" );
                primeiro();
            }   
            catch(Exception e)
            {
                JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
            }
        }
        else
        {
            oPontoVendas.setIdPonto( Integer.parseInt( tfCodigo.getText() ));

            try
            {
                // # instancia do DAO 
                PontoVendasDAO pontosVendasD = new PontoVendasDAO(); 
                
                pontosVendasD.atualizarRegistro( oPontoVendas );
                JOptionPane.showMessageDialog( this, "Atualizado com Sucesso" );
            }
            catch(Exception e)
            {
                JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
            }
        }

    }// end method 
    
}